import os
import sys
import ctypes
import subprocess

# 检查是否以管理员权限运行
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

# 以管理员权限重新运行本脚本
def run_as_admin():
    script = sys.executable
    params = ' '.join([f'"{arg}"' for arg in sys.argv])
    ctypes.windll.shell32.ShellExecuteW(None, "runas", script, params, None, 1)
    sys.exit()

# 执行清理图标缓存的命令
def clear_icon_cache():
    commands = [
        'taskkill /f /im explorer.exe',
        'attrib -h -s -r "%userprofile%\AppData\Local\IconCache.db"',
        'del "%userprofile%\AppData\Local\IconCache.db"  /f /q',
        'start explorer.exe'
    ]
    for cmd in commands:
        print(f"正在执行: {cmd}")
        subprocess.run(cmd, shell=True)
    print("图标缓存清理完成！")

if __name__ == "__main__":
    if not is_admin():
        print("正在请求管理员权限...")
        run_as_admin()
    clear_icon_cache()
    input("按回车键退出...")
